import { Component, OnInit } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
// import ngx-bootstrap modal
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class';
import { AddGroupComponent } from './../add-group-dialog/add-group.component';
import { GroupService } from './../../core/services/group.service';
import { ToasterService } from '../../share/toastr/toastr.service';


@Component({
    selector: 'app-groups',
    templateUrl: './groups.html',
    styleUrls: ['./groups.css']
})
export class GroupsComponent implements OnInit {

    groupList: any[] = []

    groups:    any[] = [];

    public modalRef: BsModalRef;

    groupSubs:       Subscription;

    constructor(
      private bsModalService: BsModalService,
      private groupService:   GroupService,
      public  toastrService:  ToasterService
    ) { }

    public addGroup() {
      this.modalRef = this.bsModalService.show(AddGroupComponent, {
        animated:            true,
        keyboard:            true,
        backdrop:            true,
        ignoreBackdropClick: true
      });
    }

    public getGroupsList() {
       this.groupService.getGroupLists().subscribe(
           (data) => {
               this.groupList = data;
               for (let i = 0; this.groupList.length; i++) {
                 this.groupList[i]['checked'] = false;
                 console.log('add checked attribute ...', this.groupList[i]['checked']);
               }
               console.log(`checked box ---`, this.groupList);
           }
        )
    }

    public onAllChecked(isChecked: boolean) {
      this.groupList.forEach((item) => {
        item.checked = isChecked;
      })
    }

    public onRowChecked($event, itemIndex) {
      this.groupList.forEach((item, index) => {
        if (index == itemIndex) {
          item.checked = !item.checked;
        }
      });
    }

    public selectedProductGroup(groupId: number) {
      let groupsId = groupId;
      this.groupSubs = this.groupService.deleteProductGroups(groupsId)
          .subscribe(
            data => {
              this.toastrService.successful('delete group successfuly ! ');
            }
          )
    }

    public groupStatus(groupSta: any) {
      console.log(groupSta, '--- group status');
      let groupsVal = groupSta;
    }


    public FilterGroupDate(groupsDate: any) {
      console.log(groupsDate, '--- filter groups date');
      let groupsDates = groupsDate;
    }

    ngOnInit() {
      this.getGroupsList();
    }
}
